package system;

import java.sql.ResultSet;
import java.sql.SQLException;

import db.BaseDBProxy;

public class UserProxy extends BaseDBProxy<User> {
	private String mTableName = "users";
	private static final String[] sColumnNames = {
		"id", "account", "password", "user_identity",
		"name", "phone", "address", "credit_points",
		"equal_coupons_values", "discount_coupons_values"
	};
	
	public UserProxy() {
		this("users");
	}
	
	public UserProxy(String pTableName) {
		mTableName = pTableName;
	}
	
	@Override
	protected String[] getTableColumnNames() {
		return sColumnNames;
	}
	
	@Override
	protected String[] getRightValues(User pItem) {
		String[] rightValues = new String[sColumnNames.length];
		rightValues[0] = String.valueOf(pItem.id);
		rightValues[1] = "'" + pItem.account + "'";
		rightValues[2] = "'" + pItem.password + "'";
		rightValues[3] = "'" + pItem.user_identity + "'";
		rightValues[4] = "'" + pItem.name + "'";
		rightValues[5] = "'" + pItem.phone + "'";
		rightValues[6] = "'" + pItem.address + "'";
		rightValues[7] = String.valueOf(pItem.credit_points);
		rightValues[8] = String.valueOf(pItem.equal_coupons_count);
		rightValues[9] = String.valueOf(pItem.id);
		
		return rightValues;
	}

	@Override
	protected User inflate(ResultSet pResultSet) throws SQLException {
		User u = new User();
		int i = 0;
		u.id = pResultSet.getLong(sColumnNames[i++]);
		u.account = pResultSet.getString(sColumnNames[i++]);
		u.password = pResultSet.getString(sColumnNames[i++]);
		u.user_identity = UserIdentity.valueOf(pResultSet.getString(sColumnNames[i++]));
		u.name = pResultSet.getString(sColumnNames[i++]);
		u.phone = pResultSet.getString(sColumnNames[i++]);
		u.address = pResultSet.getString(sColumnNames[i++]);
		u.credit_points = pResultSet.getFloat(sColumnNames[i++]);
		
		u.equal_coupons_count = pResultSet.getInt(sColumnNames[i++]);
		u.discount_coupons_count = pResultSet.getInt(sColumnNames[i++]);
		
		return u;
	}

	@Override
	protected String getTableName() {
		return mTableName;
	}
	
}
